<?php
	include "globals.php";
	
	if(!$_GET['spend']){
		print "Welcome to the crystal temple!<br />
You have <b>{$ir['crystals']}

	</b> crystals.<br />
What would you like to spend your crystals on?<br />
<br />
<a href='crystaltemple.php?spend=refill'>Energy Refill - {$set['ct_refillprice']}

 Crystals</a><br />
<a href='crystaltemple.php?spend=IQ'>IQ - {$set['ct_iqpercrys']}

 IQ per crystal</a><br />
<a href='crystaltemple.php?spend=money'>Money - \$".number_format($set['ct_moneypercrys'])." per crystal</a><br />";
} else{
	
	if($_GET['spend'] == 'refill'){
		
		if($ir['crystals'] <$set['ct_refillprice']){
			print "You don't have enough crystals!";
		} else
		if($ir['energy'] == $ir['maxenergy']){
			print "You already have full energy.";
		} else{
			$db->query("UPDATE users SET energy=maxenergy,crystals=crystals-{$set['ct_refillprice']}

		 WHERE userid=$userid");
		print "You have paid {$set['ct_refillprice']}

	 crystals to refill your energy bar.";
}

} else
if($_GET['spend'] == 'IQ'){
	print "Type in the amount of crystals you want to swap for IQ.<br />
You have <b>{$ir['crystals']}

</b> crystals.<br />
One crystal = {$set['ct_iqpercrys']}

 IQ.<form action='crystaltemple.php?spend=IQ2' method='post'><input type='text' name='crystals' /><br /><input type='submit' value='Swap' /></form>";
} else
if($_GET['spend'] == 'IQ2'){
	$_POST['crystals']=(int) $_POST['crystals'];
	
	if($_POST['crystals'] <= 0 || $_POST['crystals'] > $ir['crystals']){
		print "Error, you either do not have enough crystals or did not fill out the form.<br />
<a href='crystaltemple.php?spend=IQ'>Back</a>";
	} else{
		$iqgain=$_POST['crystals']*$set['ct_iqpercrys'];
		$db->query("UPDATE users SET crystals=crystals-{$_POST['crystals']}

	 WHERE userid=$userid");
	$db->query("UPDATE userstats SET IQ=IQ+$iqgain WHERE userid=$userid");
	print "You traded {$_POST['crystals']}

 crystals for $iqgain IQ.";
}

} else
if($_GET['spend'] == 'money'){
	print "Type in the amount of crystals you want to swap for money.<br />
You have <b>{$ir['crystals']}

</b> crystals.<br />
One crystal = \$".number_format($set['ct_moneypercrys']).".<form action='crystaltemple.php?spend=money2' method='post'><input type='text' name='crystals' /><br /><input type='submit' value='Swap' /></form>";
} else
if($_GET['spend'] == 'money2'){
	$_POST['crystals']=(int) $_POST['crystals'];
	
	if($_POST['crystals'] <= 0 || $_POST['crystals'] > $ir['crystals']){
		print "Error, you either do not have enough crystals or did not fill out the form.<br />
<a href='crystaltemple.php?spend=money'>Back</a>";
	} else{
		$iqgain=$_POST['crystals']*$set['ct_moneypercrys'];
		$db->query("UPDATE users SET crystals=crystals-{$_POST['crystals']}

	,money=money+$iqgain WHERE userid=$userid");
	print "You traded {$_POST['crystals']}

 crystals for \$".number_format($iqgain).".";
}

}

}

$h->endpage();
?>
